#include "Method.h"

Method::Method(QString name)
{
    this->name = name;
}

AccessModifier Method::getAccessModifier()
{
    return accessModifier;
}

void Method::setAccessModifier(AccessModifier am)
{
    this->accessModifier = am;
}

Clazz Method::getReturnType()
{
    return returnType;
}

void Method::setReturnType(Clazz rt)
{
    this->returnType = rt;
}

QString Method::getName()
{
    return name;
}

void Method::setName(QString name)
{
    this->name = name;
}

QVector<Parameter*> Method::getParameters()
{
    return parameters;
}

void Method::setParameters(QVector<Parameter*> parameters)
{
    this->parameters = parameters;
}

QString Method::toXml()
{
    int nr = XMLSerializable::currentNumber++;
    QString xmi_id = "UMLOperation."+QString::number(nr);
    QString visibility = (accessModifier == PROTECTED)?"protected" : ((accessModifier == PRIVATE)?"private":"public") ;
    QString isSpecification = "false";
    QString ownerScope = "instance";
    QString isQuery = "false";
    QString concurrency = "sequential";
    QString isRoot = "false";
    QString isLeaf = "false";
    QString isAbstract = "false"; //???
    QString specification = "";
    QString owner = XMLSerializable::currentOwner;


    QString ret = "";

    ret += "          <UML:Operation xmi.id=\""+xmi_id+"\" "\
           "name=\""+name+"\" "\
           "visibility=\""+visibility+"\" "\
           "isSpecification=\""+isSpecification+"\" "\
           "ownerScope=\""+ownerScope+"\" "\
           "isQuery=\""+isQuery+"\" "\
           "concurrency=\""+concurrency+"\" "\
           "isRoot=\""+isRoot+"\" "\
           "isLeaf=\""+isLeaf+"\" "\
           "isAbstract=\""+isAbstract+"\" "\
           "specification=\""+specification+"\" "\
           "owner=\""+owner+"\">\n";

    QString retType = "";
    if(!returnType.getClassName().isEmpty())
    {
        retType = XMLSerializable::typeNameToXmiId.value(returnType.getClassName(), "UMLClass."+QString::number(XMLSerializable::currentNumber++));
        XMLSerializable::typeNameToXmiId.insert(returnType.getClassName(), retType);
    }

    ret += "            <UML:BehavioralFeature.parameter>\n";
    ret += "              <UML:Parameter xmi.id=\"UML:Parameter."+QString::number(XMLSerializable::currentNumber++) +"\" name=\"\" visibility=\"public\" isSpecification=\"false\" kind=\"return\" behavioralFeature=\""+xmi_id+"\" type=\""+retType+"\" /> \n";

    for(QVector<Parameter*>::Iterator it=parameters.begin(); it!=parameters.end(); it++){
        QString type = "";

        if(!(*it)->getClazz().getClassName().isEmpty())
        {
            type = XMLSerializable::typeNameToXmiId.value((*it)->getClazz().getClassName(), "UMLClass."+QString::number(XMLSerializable::currentNumber++));
            XMLSerializable::typeNameToXmiId.insert((*it)->getClazz().getClassName(), type);
        }

        ret += "              <UML:Parameter xmi.id=\"UMLParameter."+QString::number(XMLSerializable::currentNumber++) +"\" name=\""+(*it)->getName()+"\" visibility=\"public\" isSpecification=\"false\" kind=\"in\" behavioralFeature=\""+xmi_id+"\" type=\""+type+"\" /> \n";
    }

    ret += "            </UML:BehavioralFeature.parameter>\n";
    ret += "          </UML:Operation>\n";

    return ret;
}
